package com.example.friend.service.question;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.common.core.domain.TableDataInfo;
import com.example.friend.domain.question.Question;
import com.example.friend.domain.question.dto.QuestionQueryDTO;
import com.example.friend.domain.question.es.QuestionES;
import com.example.friend.domain.question.vo.QuestionDetailVO;
import com.example.friend.domain.question.vo.QuestionVO;
import com.example.friend.elasticsearch.QuestionRepository;
import com.example.friend.manager.QuestionCacheManager;
import com.example.friend.mapper.question.QuestionMapper;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 23050
 * @version 1.0
 * @description: TODO
 * @date 2025/3/16 20:36
 */
@Service
public class QuestionServiceImpl implements QuestionService{


    @Autowired
    private QuestionRepository questionRepository;

    @Resource
    private QuestionMapper questionMapper;

    @Resource
    private QuestionCacheManager questionCacheManager;



    //查询题目列表
    @Override
    public TableDataInfo list(QuestionQueryDTO questionQueryDTO) {
        long count = questionRepository.count();
        if (count<=0){
            //将数据库中的数据同步到es中
            refreshQuestion();
        }
        Sort sort = Sort.by(Sort.Direction.DESC, "createTime");
        Pageable pageable = PageRequest.of(questionQueryDTO.getPageNum()-1,
                questionQueryDTO.getPageSize(),
                sort
                );
        Integer difficulty = questionQueryDTO.getDifficulty();
        String keyword = questionQueryDTO.getKeyword();
        Page<QuestionES> questionESPage;
        if (difficulty==null && StrUtil.isEmpty(keyword)){
            questionESPage = questionRepository.findAll(pageable);
        }else if (StrUtil.isEmpty(keyword)){
            questionESPage = questionRepository.findQuestionByDifficulty(difficulty,pageable);
        }else if(difficulty == null){
            questionESPage = questionRepository.findByTitleOrContent(keyword,keyword,pageable);
        }else{
            questionESPage = questionRepository.findByTitleOrContentAndDifficulty(keyword,keyword,difficulty,pageable);
        }
        long total = questionESPage.getTotalElements();
        if (total==0){
            return TableDataInfo.empty();
        }
        List<QuestionES> questionESList = questionESPage.getContent();
        List<QuestionVO> questionVOList = BeanUtil.copyToList(questionESList, QuestionVO.class);
        return TableDataInfo.success(questionVOList,total);

    }

    //查询题目详情
    @Override
    public QuestionDetailVO detail(Long questionId) {
        //从ES中查询
        QuestionES questionES = questionRepository.findById(questionId).orElse(null);
        QuestionDetailVO questionDetailVO = new QuestionDetailVO();
        if (questionES!=null){
            BeanUtil.copyProperties(questionES,questionDetailVO);
            return questionDetailVO;
        }

        Question question = questionMapper.selectById(questionId);
        if (question==null){
            return null;
        }
        //数据库中查玩之后，刷新es
        refreshQuestion();
        BeanUtil.copyProperties(question,questionDetailVO);
        return questionDetailVO;
    }

    //查找上一题
    @Override
    public String preQuestion(Long questionId) {
        //判断Redis中是否有数据
        Long listSize = questionCacheManager.getListSize();
        if (listSize==null || listSize==0){
            //如果Redis中没有找到，那么刷新
            questionCacheManager.refreshCache();
        }
        //从缓存中拿数据
        return questionCacheManager.preQuestion(questionId);
    }

    //查找下一题
    @Override
    public String nextQuestion(Long questionId) {
        //判断Redis中是否有数据
        Long listSize = questionCacheManager.getListSize();
        if (listSize==null || listSize==0){
            //如果Redis中没有找到，那么刷新
            questionCacheManager.refreshCache();
        }
        //从缓存中拿数据
        return questionCacheManager.nextQuestion(questionId);
    }

    //刷新ES中的题目数据数据
    private void refreshQuestion() {
        List<Question> questionList = questionMapper.selectList(new LambdaQueryWrapper<Question>());
        if (CollectionUtil.isEmpty(questionList)){
            return;
        }
        List<QuestionES> questionESList = BeanUtil.copyToList(questionList,QuestionES.class);
        questionRepository.saveAll(questionESList);
    }



}
